<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap core CSS -->
<link rel="stylesheet" type="text/css" href="../../bootstrap-3.3.7/dist/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="../../bootstrap-3.3.7/dist/css/bootstrap-theme.min.css" />
<link rel="stylesheet" type="text/css" href="../../css/index.css" />
<link rel="stylesheet" type="text/css" href="../../css/personal.css" />
<link rel="stylesheet" type="text/css" href="../../css/background.css" />
<link rel="stylesheet" type="text/css" href="../../css/bootstrapValidator.min.css" />
<script type="text/javascript" src="../../bootstrap-3.3.7/dist/js/jquery.min.js"></script>
<script type="text/javascript" src="../../bootstrap-3.3.7/dist/js/bootstrap.js"></script>
<script type="text/javascript" src="../../js/bootstrapValidator.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="../../bootstrap-3.3.7/docs/assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<link rel="stylesheet" href="../../zTree/css/metroStyle/metroStyle.css" type="text/css">
<script type="text/javascript" src="../../js/browser.js"></script>
<script type="text/javascript" src="../../zTree/js/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript" src="../../zTree/js/jquery.ztree.excheck-3.5.js"></script>
<script type="text/javascript" src="../../zTree/js/jquery.ztree.exedit-3.5.js"></script>
<script type="text/javascript">
	
</script>
</head>
<body>
		<div class="row">
			<h4 class="heading">标签配置</h4>
			<button type="button" class="btn btn-link" onclick="openAddTag()">添加标签</button>
			<button type="button" class="btn btn-link" onclick="deleteTags()">删除标签</button>
			<div class="myline" id="biaoqian"></div>
                 
		</div>
		<div class="row">
			<h4 class="heading">库标签配置</h4>
			<div class="myline" id="kubiaoqian"></div>
		</div>
		<div class="row">
			<h4 class="heading">目录配置</h4>
			<div class="myline"></div>
			<button type="button" class="btn btn-link" onclick="addTopKu()">添加顶级库</button>
			<ul id="treeDemo" class="ztree" style="background-color: #F5FFFA; border: 0px; overflow-y: auto; overflow-x: auto;"></ul>
		</div>

		<div class="modal fade" id="addTag" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						<h4 class="modal-title" id="myModalLabel">添加标签</h4>
					</div>
					<div class="modal-body">
						<input id="tag_name" name="name" type="text" class="form-control">
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
						<button type="button" class="btn btn-primary" onclick="addTopTag()">添加</button>
					</div>
				</div>
				<!-- /.modal-content -->
			</div>
			<!-- /.modal -->
		</div>

		<div class="modal fade" id="addReczskTag" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						<h4 class="modal-title" id="myModalLabel">添加库标签</h4>
					</div>
					<div class="modal-body">
						<input id="renczsk_tag_id" name="id" type="text" hidden="true">
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
						<button type="button" class="btn btn-primary" onclick="addReczskTag()">添加</button>
					</div>
				</div>
				<!-- /.modal-content -->
			</div>
			<!-- /.modal -->
		</div>

	<script type="text/javascript">
		var setting_tag = {
			view : {
				showLine : false,
				showIcon : false,
				checkable : true
			},
			edit : {
				enable : true,
				editNameSelectAll : true,
				showRemoveBtn : showRemoveBtn,
				showRenameBtn : showRenameBtn
			},
			data : {
				simpleData : {
					enable : true
				}
			},
			callback : {
				beforeDrag : beforeDrag,
				beforeEditName : beforeEditName,
				beforeRemove : beforeRemove,
				beforeRename : beforeRename,
				onRemove : onRemove,
				onRename : onRename
			}
		};
		var oldtagname;
		
		$(document).ready(initTag());
		//标签管理部分树初始化
		function initTag() {
			var taglist=[];
			$.ajax({
				async : false,
				cache : false,
				type : 'POST',
				dataType : "json",
				data : {
					"tag_id" : "",
					"name" : "标签"
				},
				url : "../configration/getTag",
				error : function() {
					alert('请求失败');
				},
				success : function(data) {
					taglist = data;

				}
			});//AJAX结束

			var object = $("#biaoqian");
			for (var i = 0; i < taglist.length; i++) {
				var addStr = "<div class='tag_div'><div class='col-md-3'><input type='checkbox' name='checktag' value='"+taglist[i].tag_id+"'>"
						+ taglist[i].name
						+ "<a class='btn btn-link' onclick=addTag('"
						+ taglist[i].tag_id
						+ "')>增加</a><ul id='tagTree_"+taglist[i].tag_id+ "' class='ztree' style='background-color: #F5FFFA; border: 0px; overflow-y: auto; overflow-x: auto;'></ul></div></div>"
				object.after(addStr);
				initTree(taglist[i].tag_id);
			}
		}
		//删除单个标签中的属性

		function addTag(treeid) {
			var zTree = $.fn.zTree.getZTreeObj("tagTree_" + treeid);
			$.ajax({
				async : false,
				cache : false,
				type : 'POST',
				dataType : "json",
				data : {
					"id" : "",
					"name" : "edit tag",
					"dictionary_id" : treeid
				},
				url : "../configration/addDictionary",
				error : function() {
					alert('请求失败');
				},
				success : function(data) {
					zTree.addNodes(null, {
						id : data.id,
						name : data.name,
						dictionary_id : data.dictionary_id
					});
					//在此处更新上层Reczsk_Tag

					$(".reczsktag_div").empty();
					initReczskTag();
				}
			});
		}
		//删除一个或多个标签

		function deleteTags() {
			var ids = [];
			var tags = document.getElementsByName("checktag");
			for (var x = 0; x < tags.length; x++) {
				if (tags[x].checked) {//选中的全部加起来  

					ids.push(tags[x].value);
				}
			}
			if (ids.length == 0) {
				alert("请选择要删除的标签");
				return;
			}
			var deleteids = ids.join(",");
			$.ajax({
				async : false,
				cache : false,
				type : 'POST',
				dataType : "json",
				data : {
					"ids" : deleteids
				},
				url : "../configration/deleteTag",
				error : function() {
					alert('请求失败');
				},
				success : function(data) {
					//此处可以更改，只删除已删除的，不再次请求数据库				

					$(".tag_div").empty();
					initTag();
					$(".reczsktag_div").empty();
					initReczskTag();
				}
			});
		}
		//当添加标签时，输入标签名字没有包含标签二字时的处理

		$("#tag_name").blur(function() {
			var name = $("#tag_name").val();
			if (!(/标签+/.test(name))) {
				$("#tag_name").val(name + "标签");
			}
		});
		function openAddTag() {
			$('#addTag').modal('show');
			$("#tag_name").val("");
		}
		//添加一个标签

		function addTopTag() {
			var name = $("#tag_name").val();
			if (name == "") {
				alert("标签名称不能为空！");
				return;
			}

			$.ajax({
				async : false,
				cache : false,
				type : 'POST',
				dataType : "json",
				data : {
					"id" : "",
					"name" : name,
					"dictionary_id" : ""
				},
				url : "../configration/addTag",
				error : function() {
					alert('请求失败');
				},
				success : function(data) {
					if (data != null) {
						$(".tag_div").empty();
						initTag();
						$('#addTag').modal('hide');
					} else {
						alert('添加失败');
					}
				}
			});
		}
		//根据id初始化zTree树

		function initTree(treeid) {
			var treeNode;
			$.ajax({
				async : false,
				cache : false,
				type : 'POST',
				dataType : "json",
				data : {
					"id" : "",
					"name" : "",
					"dictionary_id" : treeid
				},
				url : "../configration/getDictionary",
				error : function() {
					alert('请求失败');
				},
				success : function(data) {
					treeNode = data;
				}
			});
			$.fn.zTree.init($("#tagTree_" + treeid), setting_tag, treeNode);
		}

		function beforeDrag(treeId, treeNodes) {
			return false;
		}

		function beforeEditName(treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj(treeId);
			zTree.selectNode(treeNode);
			zTree.editName(treeNode);
		/* 	alert(treeNode.id);
			alert(treeNode.name);
			alert(treeNode.dictionary_id);
			alert(1111);*/
			oldtagname = treeNode.name;
			/* setTimeout(function() {

				if (confirm("您确定修改 -- " + treeNode.name + " 节点吗")) {

					setTimeout(function() {

						zTree.editName(treeNode);

					}, 0);

				}

			}, 0); */
			return false;
		}
		function beforeRemove(treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj(treeId);
			zTree.selectNode(treeNode);
			var info = confirm("您确定删除" + treeNode.name + "节点吗？");
			if (info == true)
				$.ajax({
					async : false,
					cache : false,
					type : 'POST',
					data : {
						"id" : treeNode.id,
						"name" : "",
						"dictionary_id" : ""
					},
					dataType : "json",
					url : "../configration/deleteDictionary",
					error : function() {
						alert('请求失败');
					},
					success : function(data) {
						if (data == 0) {
							alert("删除失败");
							return false;
						}
						if (data == 1)
							return true;
					}
				});
		}
		//改过了

		function onRemove(e, treeId, treeNode) {

		}
		//改过了

		function beforeRename(treeId, treeNode, newName, isCancel) {
			if (newName.length == 0) {
				setTimeout(function() {
					var zTree = $.fn.zTree.getZTreeObj(treeId);
					zTree.cancelEditName();
					alert("名字不能为空");
				}, 0);
				return false;
			} else {
				$.ajax({
					async : false,
					cache : false,
					type : 'POST',
					data : {
						"id" : treeNode.id,
						"name" : newName,
						"dictionary_id" : treeNode.dictionary_id,
						"oldname":oldtagname
					},
					dataType : "json",
					url : "../configration/updateDictionary",
					error : function() {
						alert('请求失败');
					},
					success : function(data) {
						if (data == 0) {
							alert("修改失败");
							return false;
						}
						if (data == 1)
							return true;
					}
				});
			}
		}
		//改过了

		function onRename(e, treeId, treeNode, isCancel) {
		}
		//改过了

		function showRemoveBtn(treeId, treeNode) {
			return true;
		}
		//改过了

		function showRenameBtn(treeId, treeNode) {
			return true;
		}
	</script>
	<script type="text/javascript">
		var setting_kutag = {
			view : {
				showLine : false,
				showIcon : false,
				checkable : true
			},
			edit : {
				enable : true,
				editNameSelectAll : true,
				showRemoveBtn : showRemoveBtn,
				showRenameBtn : showRenameBtn
			},
			data : {
				simpleData : {
					enable : true
				}
			},
			callback : {
				beforeDrag : beforeDrag,
				beforeRemove : beforeRemove,
				onRemove : onRemove,
			}
		};
		$(document).ready(initReczskTag());
		//标签管理部分树初始化

		function initReczskTag() {
			var taglist=[];
			$.ajax({
				async : false,
				cache : false,
				type : 'POST',
				dataType : "json",
				data : {
					"id" : "",
					"pId" : "",
					"name" : "",
					"level" : 1
				},
				url : "../configration/getReczsk",
				error : function() {
					alert('请求失败');
				},
				success : function(data) {
					taglist = data;
				}
			});//AJAX结束

			var object = $("#kubiaoqian");
			for (var i = 0; i < taglist.length; i++) {
				var addStr = "<div class='reczsktag_div'><div class='col-md-3'>"
						+ taglist[i].name
						+ "<a class='btn btn-link' onclick=openReczskTag('"
						+ taglist[i].id
						+ "')>增加</a><ul id='reczsktagTree_"+taglist[i].id+ "' class='ztree' style='background-color: #F5FFFA; border: 0px; overflow-y: auto; overflow-x: auto;'></ul></div></div>"
				object.after(addStr);
				initTreeT(taglist[i].id);
			}
		}
		//根据id初始化zTree树

		function initTreeT(treeid) {
			var treeNode;
			$.ajax({
				async : false,
				cache : false,
				type : 'POST',
				dataType : "json",
				data : {
					"tag_id" : "",
					"name" : "",
					"reczsk_id" : treeid,
					"reczsk_name" : ""
				},
				url : "../configration/getReczskTag",
				error : function() {
					alert('请求失败');
				},
				success : function(data) {
					treeNode = data;
				}
			});
			$.fn.zTree.init($("#reczsktagTree_" + treeid), setting_kutag,
					treeNode);
		}
		function openReczskTag(treeid) {
			$(".checkbox").empty();
			var addtaglist;
			$.ajax({
				async : false,
				cache : false,
				type : 'POST',
				dataType : "json",
				data : {
					"id" : treeid
				},
				url : "../configration/getReczskTagListN",
				error : function() {
					alert('请求失败');
				},
				success : function(data) {
								
					addtaglist = data;
					if(addtaglist.length==0){
						alert("请先添加标签");
						return;
					}	
					else{
						var object = $("#renczsk_tag_id");
						for (var i = 0; i < addtaglist.length; i++) {
							var addStr = "<div class='checkbox'><label><input name='addreczsktag' type='checkbox' value='"+addtaglist[i].tag_id+"'>"
									+ addtaglist[i].name + "</label></div>";
							object.after(addStr);
						}
						$('#addReczskTag').modal('show');
						$("#renczsk_tag_id").val(treeid);
					}
				}
			});
			
		}
		//给某个库增加标签

		function addReczskTag() {
			var treeid = $("#renczsk_tag_id").val();
			var zTree = $.fn.zTree.getZTreeObj("reczsktagTree_" + treeid);
			var ids = [];
			var tags = document.getElementsByName("addreczsktag");
			for (var x = 0; x < tags.length; x++) {
				if (tags[x].checked) {//选中的全部加起来  

					ids.push(tags[x].value);
				}
			}
			if (ids.length == 0) {
				alert("请选择要增加的标签");
				return;
			}
			var addids = ids.join(",");
			$('#addReczskTag').modal('hide');
			$.ajax({
				async : false,
				cache : false,
				type : 'POST',
				dataType : "json",
				data : {
					"reczsk_id" : treeid,
					"tag_ids" : addids
				},
				url : "../configration/addReczskTags",
				error : function() {
					alert('请求失败');
				},
				success : function(data) {
					if (data) {
						$(".reczsktag_div").empty();
						initReczskTag();
					} else
						alert("添加失败");
				}
			});
		}

		function beforeDrag(treeId, treeNodes) {
			return false;
		}
		function beforeRemove(treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj(treeId);
			zTree.selectNode(treeNode);
			var ids = [];
			ids = treeId.split("_");
			var info = confirm("您确定删除" + treeNode.name + "节点吗？");
			if (info == true)
				$.ajax({
					async : false,
					cache : false,
					type : 'POST',
					data : {
						"tag_id" : treeNode.tag_id,
						"name" : "",
						"reczsk_id" : ids[1],
						"reczsk_name" : ""
					},
					dataType : "json",
					url : "../configration/deleteReczskTag",
					error : function() {
						alert('请求失败');
					},
					success : function(data) {
						if (data == 0) {
							alert("删除失败");
							return false;
						}
						if (data == 1)
							return true;
					}
				});
		}
		//改过了

		function onRemove(e, treeId, treeNode) {

		}
		//改过了

		function showRemoveBtn(treeId, treeNode) {
			return true;
		}
		function showRenameBtn(treeId, treeNode) {
			return false;
		}
	</script>
	
	<script type="text/javascript">
		var setting = {
			view : {
				addHoverDom : addHoverDom,
				removeHoverDom : removeHoverDom,
				showLine : false,
				showIcon : false,
				checkable : true
			},
			edit : {
				enable : true,
				editNameSelectAll : true,
				showRemoveBtn : showRemoveBtn,
				showRenameBtn : showRenameBtn
			},
			data : {
				simpleData : {
					enable : true
				}
			},
			callback : {
				beforeDrag : beforeDrag,
				beforeEditName : beforeEditName,
				beforeRemove : beforeRemove,
				beforeRename : beforeRename,
				onRemove : onRemove,
				onRename : onRename
			}
		};

		$(document).ready(function() {
			var treeNode=[];
			$.ajax({
				async : false,
				cache : false,
				type : 'POST',
				dataType : "json",
				url : "../configration/getReczsk",
				error : function() {
					alert('请求失败');
				},
				success : function(data) {
					treeNode = data;
				}
			});
			$.fn.zTree.init($("#treeDemo"), setting, treeNode);
		});

		function beforeDrag(treeId, treeNodes) {
			return false;
		}

		function beforeEditName(treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.selectNode(treeNode);
			zTree.editName(treeNode);
			/* setTimeout(function() {
				if (confirm("您确定修改 -- " + treeNode.name + " 节点吗")) {
					setTimeout(function() {
						zTree.editName(treeNode);
					}, 0);
				}
			}, 0); */
			return false;
		}
		function beforeRemove(treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.selectNode(treeNode);
			var info = confirm("您确定删除" + treeNode.name + "节点吗？");
			var infocount;
			if (info == true) {
				//先判断库里是否有知识
				$.ajax({
					async : false,
					cache : false,
					type : 'GET',
					data : {
						"origin" : treeNode.id
					},
					dataType : "json",
					url : "../information/getkuInfoCount",
					error : function() {
						alert('请求失败');
					},
					success : function(data) {
						infocount=data;
					}
				});//AJAX结束
				if (infocount > 0) {
					alert("该库中有知识，请将知识迁移或删除后再删除该库");
					return false;
				}
				if (infocount == 0){
					$.ajax({
						async : false,
						cache : false,
						type : 'POST',
						data : {
							"id" : treeNode.id,
							"pId" : treeNode.id,
							"name" : treeNode.name,
							"level" : treeNode.level + 1
						},
						dataType : "json",
						url : "../configration/deleteReczsk",
						error : function() {
							alert('请求失败');
							return false;
						},
						success : function(data) {
							if (data == 0) {
								alert("删除失败");
								return false;
							}
							if (data == 1){
								alert("删除成功！");
								if(treeNode.level==0){
									$(".reczsktag_div").empty();
									initReczskTag();
								}
								return true;
							}
						}
					});//AJAX结束
				}
			} 
			else return false;
		}
		//改过了

		function onRemove(e, treeId, treeNode) {

		}
		//改过了

		function beforeRename(treeId, treeNode, newName, isCancel) {
			if (newName.length == 0) {
				setTimeout(function() {
					var zTree = $.fn.zTree.getZTreeObj("treeDemo");
					zTree.cancelEditName();
					alert("名字不能为空");
				}, 0);
				return false;
			} else {
				$.ajax({
					async : false,
					cache : false,
					type : 'POST',
					data : {
						"id" : treeNode.id,
						"pId" : treeNode.id,
						"name" : newName,
						"level" : treeNode.level + 1
					},
					dataType : "json",
					url : "../configration/updateReczsk",
					error : function() {
						alert('请求失败');
					},
					success : function(data) {
						if (data == 0) {
							alert("修改失败");
							return false;
						}
						if (data == 1){
							if(treeNode.level==0){
								$(".reczsktag_div").empty();
								initReczskTag();
							}			
							return true;}
					}
				});
			}
		}
		//改过了

		function onRename(e, treeId, treeNode, isCancel) {
		}
		//改过了

		function showRemoveBtn(treeId, treeNode) {
			return !treeNode.isParent;
		}
		//改过了

		function showRenameBtn(treeId, treeNode) {
			return true;
		}

		function addHoverDom(treeId, treeNode) {
			var sObj = $("#" + treeNode.tId + "_span");
			if (treeNode.editNameFlag
					|| $("#addBtn_" + treeNode.tId).length > 0)
				return;
			var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
					+ "' title='add node' onfocus='this.blur();'></span>";
			sObj.after(addStr);
			var btn = $("#addBtn_" + treeNode.tId);
			btn.bind("click", function() {
				var zTree = $.fn.zTree.getZTreeObj("treeDemo");
				$.ajax({
					async : false,
					cache : false,
					type : 'POST',
					data : {
						"id" : "",
						"pId" : treeNode.id,
						"name" : "edit node",
						"level" : treeNode.level + 2
					},
					dataType : "json",
					url : "../configration/addReczsk",
					error : function() {
						alert('请求失败');
					},
					success : function(data) {
						if (data == null)
							alert("修改失败");
						if (data != null)
							zTree.addNodes(treeNode, {
								id : data.id,
								pId : data.pId,
								name : data.name,
								level : data.level
							});
					}
				});
				return false;
			});
		};
		function addTopKu() {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			$.ajax({
				async : false,
				cache : false,
				type : 'POST',
				data : {
					"id" : "",
					"pId" : "",
					"name" : "edit node",
					"level" : 1
				},
				dataType : "json",
				url : "../configration/addReczsk",
				error : function() {
					alert('请求失败');
				},
				success : function(data) {
					if (data == null)
						alert("添加失败");
					if (data != null) {
						zTree.addNodes(null, {
							id : data.id,
							pId : data.pId,
							name : data.name,
							level : data.level
						});
						$(".reczsktag_div").empty();
						initReczskTag();
					}
				}
			});
		}
		//改过了

		function removeHoverDom(treeId, treeNode) {
			$("#addBtn_" + treeNode.tId).unbind().remove();
		};
	</script>
</body>
</html>